package cn.itsource.hrm.feign.interceptor;

import cn.itsource.hrm.constant.AuthConstant;
import cn.itsource.hrm.utils.HttpUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

import static cn.itsource.hrm.feign.interceptor.DefaultOAuth2FeignRequestInterceptor.HEAD_TOKEN;

@Component
public class OAuth2FeignRequestInterceptor implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 自己给自己颁发一个token
        String url = String.format(AuthConstant.TOKEN_URL, "temp", "123");
        Map<String, String> map = HttpUtil.sendPost(url);

        String token = map.get("access_token");
        if(StringUtils.isNotEmpty(token)){
            //把token放到feign的请求头中
            requestTemplate.header(HEAD_TOKEN,"Bearer "+token);
        }
   }
}